<template>
	<view class="login-page">
		<view class="logo-area">
			<view>
				<image src="/static/logo.png" alt="Logo" class="logo"></image>
			</view>
			<text class="title">快来跟我一起来宠喵~</text>
		</view>

		<uni-forms ref="userForm" :modelValue="user" :rules="checkUser" class="form-area">
			<uni-forms-item name="username">
				<uni-easyinput :inputBorder=false prefixIcon="person-filled" v-model="user.username"
					placeholder="请输入用户名"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="password">
				<uni-easyinput type="password" :clearable=false :inputBorder=false prefixIcon="locked-filled"
					v-model="user.password" placeholder="请输入密码"></uni-easyinput>
			</uni-forms-item>
		</uni-forms>

		<view class="btn-area">
			<button class="login-btn" @click="login">登&nbsp;录</button>
			<view class="forget-password" @click="forgetPassword">忘记密码？</view>
		</view>
		<view class="other-methods">
			<view class="line-box">
				<text class="line"></text>
				<text class="text">其他方式登录</text>
				<text class="line"></text>
			</view>
			<view class="btn-box">
				<button class="login-btn" style="background-color: #daffdf" @click="register">
					<uni-icons type="weixin" size="30" color="#4fe130" />
				</button>
				<button class="login-btn" style="background-color: #d6f0ff;" @click="register">
					<uni-icons type="phone-filled" size="30" color="#4795ff" />
				</button>
			</view>
		</view>
		<view class="register-link">
			<text style="color: #acacac;">没有账号，</text>
			<text style="color: #fdc100;" @click="register">注册新用户？</text>
		</view>
		<view style="flex: 1; background-color: #fafafa;"></view>
	</view>
</template>
<script>
	import {
		setToken
	} from '../../store/token';
	export default {
		data() {
			return {
				user: {
					username: '',
					password: ''
				},
				checkUser: {
					username: {
						rules: [{
							required: true,
							errorMessage: '请输入用户名',
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请输入密码',
						}]
					}
				}
			};
		},
		methods: {
			login() {
				this.$refs.userForm.validate().then(result => {
					this.$service({
						url: '/users/login',
						method: 'POST',
						data: this.user
					}).then(res => {
						if (res.code == 0) {
							setToken(res.data.token)
							uni.navigateBack()
						}
					})
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},
			register() {
				uni.showToast({
					title: "暂不支持此功能",
					icon: "none"
				})
			},
			forgetPassword() {
				uni.showToast({
					title: "请联系管理员处理",
					icon: "none"
				})
			}
		}
	};
</script>

<style lang="scss">
	.login-page {
		background-color: #fdd300;
		height: 100vh;
		display: flex;
		flex-direction: column;
	}

	.logo-area {
		background-color: #fdd300;
		text-align: center;
		padding: 6vmin;

		.logo {
			width: 20vmin;
			height: 20vmin;
		}

		.title {
			font-weight: bold;
		}
	}

	.form-area {
		background-color: #fafafa;
		border-radius: 30px 30px 0 0;
		padding: 60px 30px 30px 30px;
	}

	.form-area input {
		border: 1px solid #ccc;
		padding: 3px;
	}

	.btn-area {
		background-color: #fafafa;
		padding: 20px;
		text-align: center;

		.login-btn {
			background-color: #fdd300;
			color: #000;
			font-size: 18px;
			border-radius: 50px;
			padding: 10px 30px;
		}

		.forget-password {
			color: #fdc100;
			margin-top: 3vh;
		}
	}

	.other-methods {
		background-color: #fafafa;

		.line-box {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 2vh 0;

			.text {
				color: #acacac;
				font-size: 1rem;
				text-align: center;
				margin: 0 2vw;
			}

			.line {
				width: 20%;
				height: 1px;
				background-color: #e7e7e7;
			}
		}

		.btn-box {
			display: flex;
			padding: 0 10vw;

			.login-btn {
				width: 15vmin;
				height: 15vmin;
				line-height: 15vmin;
				border-radius: 100%;
			}
		}
	}

	.register-link {
		background-color: #fafafa;
		padding: 30px;
		text-align: center;
	}
</style>